package com.cloud.controller;

import com.cloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @BelongsProject: my
 * @BelongsPackage: com.cloud.controller
 * @Author: ji ge
 * @CreateTime: 2024-01-31  10:37
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("/consumer")
//hystrix fallback
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderHystrixController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/payment/hystrix/ok/{id}")
    public String paymentHystrixOk(@PathVariable("id") Integer id){
        return  paymentHystrixService.paymentHystrixOk(id);
    }

    @GetMapping("/payment/hystrix/timeout/{id}")
//    原版 hystrix-command 注解是注释的
//isolation 隔离
//    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMillions",value = "500")
//    })
    @HystrixCommand
    public String paymentHystrixTimeOut(@PathVariable("id") Integer id){
        return  paymentHystrixService.paymentHystrixTimeOut(id);
    }
    public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id) {
        return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
    }

    /**
     * hystrix 全局fallback方法
     *
     * @return
     */
    public String payment_Global_FallbackMethod() {
        return "Global异常处理信息，请稍后再试，/(ㄒoㄒ)/~~";
    }
}
